#include "SceneManager.hpp"

SceneManager::SceneManager() {
    current = NULL;
}

void SceneManager::Update(sf::RenderWindow& app, sf::Event& event) {
    if (current != NULL) {
        current->Update(event, app);
        current->Draw(app);
    }
}

void SceneManager::Add(std::string sceneName, IScene& scene) {
    scene.manager = this;
    sceneList.insert(std::pair<std::string, IScene&>(sceneName, scene));
}

void SceneManager::Remove(std::string sceneName) {
    sceneList.erase(sceneName);
}

IScene* SceneManager::Get(std::string sceneName) {
    std::map<std::string, IScene&>::iterator it;
    it = sceneList.find(sceneName);

    if (it != sceneList.end()) {
        return &it->second;
    } else {
        return NULL;
    }
}

void SceneManager::Switch(std::string sceneName) {
    std::map<std::string, IScene&>::iterator it;
    it = sceneList.find(sceneName);

    if (it != sceneList.end()) {
        // scene found. lets switch scene
        IScene& scene = it->second;

        // upload current scene
        if (current != NULL) {
            current->Unload();
        }

        scene.Load();
        current = &scene;
    }
}
